package com.scholarship.modules.student.service;

import com.scholarship.modules.student.dto.applicationDto.*;
import com.scholarship.modules.student.mapper.StuApplicationMapper;
import com.scholarship.modules.student.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: huanghao
 * @date: 2021/5/24 14:33
 * @description:
 * @modifiedBy:
 */

@Service
public class StuApplicationService {
    @Autowired
    StuApplicationMapper stuApplicationMapper;
    @Autowired
    StudentMapper studentMapper;

    //日期转换，前端是真的捞，一个日期转换都不会，fuck
    private String dateTransform(String date) {
        return date.substring(0, 10);
    }

    /*------------------------奖学金插入功能----------------------------*/
    //插入外语成绩加分
    public int insertForeignExam(ForeignExamDto foreignExamDto) {
        String userId = foreignExamDto.getUserId();
        try {
            //TODO 以后要考虑下到底要不要把CET成绩从a2表里分离出来
            //CET考试都会有的，就算没考也是0分
            ForeignExamDto cetDto = new ForeignExamDto();
            cetDto.setUserId(foreignExamDto.getUserId());
            cetDto.setExamScore((float)foreignExamDto.getCET4());
            int ret = -1;
            ret = studentMapper.updateCET4(cetDto);
            if (ret <= 0)
                studentMapper.insertCET4(cetDto);
            cetDto.setExamScore((float)foreignExamDto.getCET6());
            ret = studentMapper.updateCET6(cetDto);
            if (ret <= 0)
                studentMapper.insertCET6(cetDto);

            //如果存在额外的外语考试，才插入成绩
            String examName = foreignExamDto.getExamName();

            if (examName != null && !"".equals(examName)) {
                if (foreignExamDto.getExamTime() == null)
                    foreignExamDto.setExamTime("2020-01-01 00:00:00");
                if (foreignExamDto.getExamScore() == null)
                    foreignExamDto.setExamScore((float)0);
                String examTime = foreignExamDto.getExamTime();
                //日期转换
                examTime = dateTransform(examTime);
                foreignExamDto.setExamTime(examTime);

                ret = stuApplicationMapper.updateForeignExam(foreignExamDto);
                if (ret <= 0)
                    stuApplicationMapper.insertForeignExam(foreignExamDto);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    //插入专业证书加分
    public int insertMajorExam(MajorExamDto majorExamDto) {
        try {
            if (majorExamDto.getCertificateName() == null || "".equals(majorExamDto.getCertificateName()))
                return 0;

            //日期转换
            majorExamDto.setExamTime(dateTransform(majorExamDto.getExamTime()));
            int ret = stuApplicationMapper.updateMajorExam(majorExamDto);
            if (ret <= 0)
                stuApplicationMapper.insertMajorExam(majorExamDto);
        }
        catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

        return 1;
    }

    //插入专业竞赛加分
    public int insertMajorCompetition(MajorCompetitionDto majorCompetitionDto) {
        try {
            if (majorCompetitionDto.getCompetitionName() == null || "".equals(majorCompetitionDto.getCompetitionName()))
                return 0;
            if (majorCompetitionDto.getRole() == null || majorCompetitionDto.getRole().equals(""))
                majorCompetitionDto.setIsRole("0");
            else
                majorCompetitionDto.setIsRole("1");
            //日期转换
            majorCompetitionDto.setCompetitionTime(dateTransform(majorCompetitionDto.getCompetitionTime()));
            int ret = stuApplicationMapper.updateMajorCompetitionInfo(majorCompetitionDto);
            if (ret <= 0)
                stuApplicationMapper.insertMajorCompetitionInfo(majorCompetitionDto);
        }
        catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    //
    //插入IT节加分,复用了专业竞赛的Dto
    //TODO 可考虑合并IT加分和专业竞赛加分，因为他们的字段一样，只是插入的表不一样
    public int insertITCompetition(MajorCompetitionDto majorCompetitionDto) {
        try {
            if (majorCompetitionDto.getCompetitionName() == null || "".equals(majorCompetitionDto.getCompetitionName()))
                return 0;
            if (majorCompetitionDto.getRole() == null || majorCompetitionDto.getRole().equals(""))
                majorCompetitionDto.setIsRole("0");
            else
                majorCompetitionDto.setIsRole("1");
            //日期转换
            majorCompetitionDto.setCompetitionTime(dateTransform(majorCompetitionDto.getCompetitionTime()));

            int ret = stuApplicationMapper.updateITCompetitionInfo(majorCompetitionDto);
            if (ret <= 0)
                stuApplicationMapper.insertITCompetitionInfo(majorCompetitionDto);
        }
        catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    //把文件地址插入到数据库
    public Integer insertFile(String userId,String fileUrl,Integer tableId,String import_year) {
        return stuApplicationMapper.insertFile(userId, fileUrl, tableId,import_year);
    }
    //插入专业论文信息
    public int insertMajorThesis(MajorThesisDto majorThesisDto) {
        try {
            if (majorThesisDto.getPublicationName() == null || "".equals(majorThesisDto.getPublicationName()))
                return 0;
            //日期转换
            majorThesisDto.setThesisTime(dateTransform(majorThesisDto.getThesisTime()));
            System.out.println(majorThesisDto.getRanking());
            int ret = stuApplicationMapper.insertMajorThesisInfo(majorThesisDto);
            if (ret <= 0)
                stuApplicationMapper.insertMajorThesisInfo(majorThesisDto);
        }
        catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    //插入发明专利信息
    public int insertPatent(PatentDto patentDto, int maxLength) {
        try {
            if (patentDto.getPatentName() == null || "".equals(patentDto.getPatentName()))
                return 0;
            if (patentDto.getPatentIntroduction().length() > maxLength)
                return 0;
            patentDto.setApplyTime(dateTransform(patentDto.getApplyTime()));

            int ret = stuApplicationMapper.updatePatentInfo(patentDto);
            if (ret <= 0)
                stuApplicationMapper.insertPatentInfo(patentDto);
        }
        catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
    /*------------------------奖学金查询功能----------------------------*/
    public double getGPA(String userId) {
        return stuApplicationMapper.getGPA(userId);
    }

    //TODO:获取外语成绩加分
    @Deprecated
    public ForeignExamDto getForeignExam(String userId) {
//        ForeignExamDto foreignExamDto;
//        foreignExamDto = stuApplicationMapper.getForeignExam(userId);
//        if (foreignExamDto == null)
//            foreignExamDto = new ForeignExamDto();
////        int CET4 = studentMapper.getCET4ById(userId);
////        int CET6 = studentMapper.getCET6ById(userId);
//        foreignExamDto.setCET4(0);
//        foreignExamDto.setCET6(0);
//        List<A2> list = rewardTempMapper.listA2ByuserId(userId);
//        for(A2 a2 : list) {
//            if(a2.getExamName().equals("CET4")) {
//                foreignExamDto.setCET4(a2.getExamScore().intValue());
//            }else if (a2.getExamName().equals("CET6")) {
//                foreignExamDto.setCET6(a2.getExamScore().intValue());
//            }
//        }
        return null;
    }

    //获取专业证书加分
    public MajorExamDto getMajorExam(String userId) {
        return stuApplicationMapper.getMajorExam(userId);
    }

    //获取专业竞赛加分
    public MajorCompetitionDto getMajorCompetition(String userId) {
        return stuApplicationMapper.getMajorCompetitionInfo(userId);
    }

    //获取IT节加分
    public MajorCompetitionDto getITCompetition(String userId) {
        return stuApplicationMapper.getITCompetitionInfo(userId);
    }

    //获取专业论文信息
    public MajorThesisDto getMajorThesis(String userId) {
        return stuApplicationMapper.getMajorThesisInfo(userId);
    }

    //获取发明专利信息
    public PatentDto getPatent(String userId) {
        return stuApplicationMapper.getPatentInfo(userId);
    }

    /*------------------------奖学金删除功能----------------------------*/

    //删除外语成绩、专业竞赛等信息（CET成绩除外），撤销奖学金申请
    public int deleteApplication(String userId) {
        //TODO 写了这个部分，但还没测试，6/8
        try {
            stuApplicationMapper.deleteForeignExam(userId);
            stuApplicationMapper.deleteITCompetitionInfo(userId);
            stuApplicationMapper.deleteMajorCompetitionInfo(userId);
            stuApplicationMapper.deleteMajorExam(userId);
            stuApplicationMapper.deleteMajorThesisInfo(userId);
            stuApplicationMapper.deletePatentInfo(userId);
        }
        catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

        return 1;
    }
}
